'lowach','stanmath','efficacypre','efficacypst','psolvepre','psolvepst')
##################################################
# fit model with mcmc (ex16.1.imp)
##################################################
mymodel <- rblimp(
data = problemsolving2level,
clusterid = 'school',
ordinal = 'condition hispanic frlunch',
fixed = 'condition psolvepre',
center = 'groupmean = psolvepre hispanic frlunch; grandmean = psolvepre.mean hispanic.mean frlunch.mean',
model = 'psolvepst ~ psolvepre hispanic frlunch psolvepre.mean hispanic.mean frlunch.mean condition',
seed = 90291,
burn = 5000,
iter = 10000)
mymodel@output
##################################################
# fit model and save imputations (ex16.2.imp)
##################################################
mymodel <- rblimp(
data = problemsolving2level,
clusterid = 'school',
ordinal = 'condition hispanic frlunch',
fixed = 'condition psolvepre',
center = 'groupmean = psolvepre hispanic frlunch; grandmean = psolvepre.mean hispanic.mean frlunch.mean',
model = 'psolvepst ~ psolvepre hispanic frlunch psolvepre.mean hispanic.mean frlunch.mean condition',
seed = 90291,
burn = 5000,
iter = 10000,
nimps = 20,
chains = 20)
mymodel@output
# mitml list
implist <- as.mitml(mymodel)
# within-cluster (group mean) center level-1 predictors and add cluster-specific group means to the data
for (i in 1:length(implist)) {
implist[[i]] <- gmc(implist[[i]], x = c('psolvepre','hispanic','frlunch'), by = c('school'), FUN = mean, suffix = c('.meanj', '.cwc'), fulldataframe = TRUE)
}
# analysis and pooling
fit <- with(implist, lmer(psolvepst ~ psolvepre.cwc + hispanic.cwc + frlunch.cwc + psolvepre.meanj + hispanic.meanj + frlunch.meanj + condition + (1 | school), REML = T))
# significance tests
estimates <- testEstimates(fit, extra.pars = T)
estimates
confint(estimates)
##################################################
# wald and likelihood ratio (omnibus) tests
##################################################
# wald test that all slopes = 0
nullanalysis <- with(implist, lmer(psolvepst ~ 1 + (1 | school)))
testModels(fit, nullanalysis, method = 'D1')
# MCMC Two-Level Moderated Regression With Random Slopes
library(fdir)
library(rblimp)
library(rockchalk)
library(lme4)
library(mitml)
##################################################
# read data
##################################################
# set working directory
set()
# read imputed data from working directory
problemsolving2level <- read.table('problemsolving2level.dat', na.strings = '999')
# MCMC Two-Level Moderated Regression With Random Slopes
library(fdir)
library(rblimp)
library(rockchalk)
library(lme4)
library(mitml)
##################################################
# read data
##################################################
# set working directory
set()
# read imputed data from working directory
problemsolving2level <- read.table('problemsolving2level.dat', na.strings = '999')
# MCMC Two-Level Moderated Regression With Random Slopes
library(fdir)
library(rblimp)
library(rockchalk)
library(lme4)
library(mitml)
##################################################
# read data
##################################################
# set working directory
set()
# read imputed data from working directory
problemsolving3level <- read.table('problemsolving3level.dat', na.strings = '999')
# read imputed data from working directory
problemsolving3level <- read.table('problemsolving3level.dat', na.strings = '999')
# MCMC Two-Level Moderated Regression With Random Slopes
library(fdir)
library(rblimp)
library(rockchalk)
library(lme4)
library(mitml)
##################################################
# read data
##################################################
# set working directory
set()
# read imputed data from working directory
problemsolving3level <- read.table('problemsolving3level.dat', na.strings = '999')
# read imputed data from working directory
problemsolving3level <- read.table('problemsolving3level.dat', na.strings = '999')
# read imputed data from working directory
problemsolving3level <- read.table('problemsolving3level.dat', na.strings = '999')
# MCMC Two-Level Moderated Regression With Random Slopes
library(fdir)
library(rblimp)
library(rockchalk)
library(lme4)
library(mitml)
##################################################
# read data
##################################################
# set working directory
set()
# read imputed data from working directory
problemsolving3level <- read.table('problemsolving3level.dat', na.strings = '999')
# read imputed data from working directory
problemsolving3level <- read.table('problemsolving3level.dat')
# read imputed data from working directory
problemsolving3level <- read.table('problemsolving3level.dat')
# MCMC Two-Level Moderated Regression With Random Slopes
library(fdir)
library(rblimp)
library(rockchalk)
library(lme4)
library(mitml)
##################################################
# read data
##################################################
# set working directory
set()
# read imputed data from working directory
problemsolving3level <- read.table('problemsolving3level.dat')
names(problemsolving3level) <- c('school','student','wave','condition','teachexp','eslpct','hispanic',
'male','frlunch','lowach','stanmath','month', 'month7', 'probsolve', 'efficacy')
mymodel <- rblimp(
data = problemsolving3level,
clusterid = 'student',
ordinal = 'hispanic frlunch condition',
fixed = 'month condition',
center = 'grandmean = frlunch;',
model = '
probsolve ~ month hispanic frlunch condition month*condition month*hispanic | month',
simple = 'month | condition; month | hispanic',
seed = 90291,
burn = 10000,
iter = 20000)
mymodel@output
mymodel <- rblimp(
data = problemsolving3level,
clusterid = 'student',
ordinal = 'hispanic frlunch condition',
fixed = 'month condition',
center = 'grandmean = frlunch;',
model = '
probsolve ~ month hispanic frlunch condition month*condition month*hispanic | month',
simple = 'month | condition; month | hispanic',
seed = 90291,
burn = 10000,
iter = 20000,
nimps = 20,
chains = 20)
# MCMC Moderated Regression
library(fdir)
library(rblimp)
library(mitml)
##################################################
# read data
##################################################
# set working directory
set()
# read imputed data from working directory
behaviorachievement <- read.table('behaviorachievement.dat', na.strings = 999)
names(behaviorachievement) <- c('id','male','hispanic','riskgrp','atrisk','behsymp1','lrnprob1',
'read1','read2','read3','read9','read9grp','stanread7',
'math1','math2','math3','math9','math9grp','stanmath7')
##################################################
# fit model with mcmc (ex10.1.imp)
##################################################
mymodel <- rblimp(
data = behaviorachievement,
ordinal = 'atrisk',
center = 'read1 lrnprob1 atrisk',
model = '
focal.model:
read9 ~ read1 lrnprob1 read1*lrnprob1 atrisk ;
auxiliary.models:
stanread7 read2  ~ read9 read1 lrnprob1 atrisk',
simple = 'read1 | lrnprob1',
seed = 90291,
burn = 5000,
iter = 10000)
mymodel@output
##################################################
# fit model and save imputations (ex10.2.imp)
##################################################
mymodel <- rblimp(
data = behaviorachievement,
ordinal = 'atrisk',
center = 'read1 lrnprob1 atrisk',
model = '
focal.model:
read9 ~ read1 lrnprob1 read1*lrnprob1 atrisk ;
auxiliary.models:
stanread7 read2  ~ read9 read1 lrnprob1 atrisk',
simple = 'read1 | lrnprob1',
seed = 90291,
burn = 5000,
iter = 10000,
nimps = 20,
chains = 20)
mymodel@output
# mitml list
implist <- as.mitml(mymodel)
##################################################
# analysis and pooling
##################################################
# center lower-order variables and compute the product
mean_read1 <- mean(unlist(lapply(implist, function(df) mean(df$read1))))
mean_lrnprob1 <- mean(unlist(lapply(implist, function(df) mean(df$lrnprob1))))
mean_atrisk <- mean(unlist(lapply(implist, function(df) mean(df$atrisk))))
for (i in 1:length(implist)) {
implist[[i]]$read1.cgm <- implist[[i]]$read1 - mean_read1
implist[[i]]$lrnprob1.cgm <- implist[[i]]$lrnprob1 - mean_lrnprob1
implist[[i]]$atrisk.cgm <- implist[[i]]$atrisk - mean_atrisk
}
# analysis and pooling
fit <- with(implist, lm(read9 ~ read1.cgm + lrnprob1.cgm + read1.cgm:lrnprob1.cgm + atrisk.cgm))
# significance tests with barnard & rubin degrees of freedom
estimates <- testEstimates(fit, extra.pars = T, df.com = 133)
estimates
confint(estimates) # confidence intervals
# pooled r-square
rsquare <- mean(unlist(lapply(implist, (function(x) summary(lm(read9 ~ read1.cgm + lrnprob1.cgm + read1.cgm:lrnprob1.cgm + atrisk.cgm, data = x))$r.squared))))
rsquare
lrnprob1.sd <- mean(unlist(lapply(implist, (function(x) sd(x$lrnprob1.cgm)))))
lrnprob1.sd
# test conditional effect (simple slope) at +1 SD above the mean
slp_high <- 'read1.cgm + read1.cgm:lrnprob1.cgm*1*10.77'
testConstraints(fit, constraints = slp_high, df.com = 133)
# MCMC Two-Level Moderated Regression With Random Slopes
library(fdir)
library(rblimp)
library(rockchalk)
library(lme4)
library(mitml)
##################################################
# read data
##################################################
# set working directory
set()
# read imputed data from working directory
problemsolving3level <- read.table('problemsolving3level.dat')
names(problemsolving3level) <- c('school','student','wave','condition','teachexp','eslpct','hispanic',
'male','frlunch','lowach','stanmath','month', 'month7', 'probsolve', 'efficacy')
##################################################
# fit model with mcmc (ex17.1.imp)
##################################################
mymodel <- rblimp(
data = problemsolving3level,
clusterid = 'student',
ordinal = 'hispanic frlunch condition',
fixed = 'month condition',
center = 'grandmean = frlunch;',
model = '
probsolve ~ month hispanic frlunch condition month*condition month*hispanic | month',
simple = 'month | condition; month | hispanic',
seed = 90291,
burn = 10000,
iter = 20000)
mymodel@output
##################################################
# fit model and save imputations (ex17.2.imp)
##################################################
mymodel <- rblimp(
data = problemsolving3level,
clusterid = 'student',
ordinal = 'hispanic frlunch condition',
fixed = 'month condition',
center = 'grandmean = frlunch;',
model = '
probsolve ~ month hispanic frlunch condition month*condition month*hispanic | month',
simple = 'month | condition; month | hispanic',
seed = 90291,
burn = 10000,
iter = 20000,
nimps = 20,
chains = 20)
mymodel <- rblimp(
data = problemsolving3level,
clusterid = 'student',
ordinal = 'hispanic frlunch condition',
fixed = 'month condition',
center = 'grandmean = frlunch;',
model = '
probsolve ~ month hispanic frlunch condition month*condition month*hispanic | month',
simple = 'month | condition; month | hispanic',
seed = 90291,
burn = 10000,
iter = 20000,
nimps = 20,
chains = 20)
# MCMC Two-Level Moderated Regression With Random Slopes
library(fdir)
library(rblimp)
library(rockchalk)
library(lme4)
library(mitml)
##################################################
# read data
##################################################
# set working directory
set()
# read imputed data from working directory
problemsolving3level <- read.table('problemsolving3level.dat', na.strings = '999')
names(problemsolving3level) <- c('school','student','wave','condition','teachexp','eslpct','hispanic',
'male','frlunch','lowach','stanmath','month', 'month7', 'probsolve', 'efficacy')
##################################################
# fit model with mcmc (ex17.1.imp)
##################################################
mymodel <- rblimp(
data = problemsolving3level,
clusterid = 'student',
ordinal = 'hispanic frlunch condition',
fixed = 'month condition',
center = 'grandmean = frlunch;',
model = '
probsolve ~ month hispanic frlunch condition month*condition month*hispanic | month',
simple = 'month | condition; month | hispanic',
seed = 90291,
burn = 10000,
iter = 20000)
mymodel@output
mymodel <- rblimp(
data = problemsolving3level,
clusterid = 'student',
ordinal = 'hispanic frlunch condition',
fixed = 'month condition',
center = 'grandmean = frlunch;',
model = '
probsolve ~ month hispanic frlunch condition month*condition month*hispanic | month',
simple = 'month | condition; month | hispanic',
seed = 90291,
burn = 10000,
iter = 20000,
nimps = 20,
chains = 20)
mymodel@output
# mitml list
implist <- as.mitml(mymodel)
##################################################
# analysis and pooling
##################################################
# analysis and pooling
fit <- with(implist, lmer(probsolve ~ month + frlunch.cgm + hispanic + condition + month:condition + month:hispanic + (1 + month | student), REML = T))
fit <- with(implist, lmer(probsolve ~ month7  + male.cgm + frlunch + teachexp.cgm + condition + product + (1 + month7 | school/student), REML = T))
# analysis and pooling
fit <- with(implist, lmer(probsolve ~ month + frlunch + hispanic + condition + month:condition + month:hispanic + (1 + month | student), REML = T))
# significance tests
estimates <- testEstimates(fit, extra.pars = T)
estimates
confint(estimates) # confidence intervals
# test conditional effect (simple slope) at condition = 0
testConstraints(fit, constraints = 'month + month:condition*0')
# test conditional effect (simple slope) at condition = 1
testConstraints(fit, constraints = 'month + month:condition*1')
# test conditional effect (simple slope) at hispanic = 0
testConstraints(fit, constraints = 'month + month:hispanic*0')
# test conditional effect (simple slope) at hispanic = 1
testConstraints(fit, constraints = 'month + month:hispanic*1')
# test conditional effect (simple slope) at condition = 1
testConstraints(fit, constraints = 'month + month:condition*1')
# test conditional effect (simple slope) at hispanic = 0
testConstraints(fit, constraints = 'month + month*hispanic*0')
# test conditional effect (simple slope) at hispanic = 0
testConstraints(fit, constraints = 'month + month:hispanic*0')
testConstraints(fit, constraints = 'month + month*condition*0')
# test conditional effect (simple slope) at condition = 1
testConstraints(fit, constraints = 'month + month*condition*1')
# test conditional effect (simple slope) at hispanic = 0
testConstraints(fit, constraints = 'month + month*hispanic*0')
# test conditional effect (simple slope) at hispanic = 1
testConstraints(fit, constraints = 'month + month*hispanic*1')
# MCMC Moderated Regression
library(fdir)
library(rblimp)
library(mitml)
##################################################
# read data
##################################################
# set working directory
set()
# read imputed data from working directory
behaviorachievement <- read.table('behaviorachievement.dat', na.strings = 999)
names(behaviorachievement) <- c('id','male','hispanic','riskgrp','atrisk','behsymp1','lrnprob1',
'read1','read2','read3','read9','read9grp','stanread7',
'math1','math2','math3','math9','math9grp','stanmath7')
##################################################
# fit model with mcmc (ex10.1.imp)
##################################################
mymodel <- rblimp(
data = behaviorachievement,
ordinal = 'atrisk',
center = 'read1 lrnprob1 atrisk',
model = '
focal.model:
read9 ~ read1 lrnprob1 read1*lrnprob1 atrisk ;
auxiliary.models:
stanread7 read2  ~ read9 read1 lrnprob1 atrisk',
simple = 'read1 | lrnprob1',
seed = 90291,
burn = 5000,
iter = 10000)
mymodel@output
##################################################
# fit model and save imputations (ex10.2.imp)
##################################################
mymodel <- rblimp(
data = behaviorachievement,
ordinal = 'atrisk',
center = 'read1 lrnprob1 atrisk',
model = '
focal.model:
read9 ~ read1 lrnprob1 read1*lrnprob1 atrisk ;
auxiliary.models:
stanread7 read2  ~ read9 read1 lrnprob1 atrisk',
simple = 'read1 | lrnprob1',
seed = 90291,
burn = 5000,
iter = 10000,
nimps = 20,
chains = 20)
mymodel@output
# mitml list
implist <- as.mitml(mymodel)
for (i in 1:length(implist)) {
implist[[i]]$ones <- 1
implist[[i]] <- gmc(implist[[i]], x = c('read1','learnprob1','atrisk'), by = c('ones'), FUN = mean, suffix = c('.mean', '.cgm'), fulldataframe = TRUE)
}
head(implist[[i]])
for (i in 1:length(implist)) {
implist[[i]]$ones <- 1
implist[[i]] <- gmc(implist[[i]], x = c('read1','lrnprob1','atrisk'), by = c('ones'), FUN = mean, suffix = c('.mean', '.cgm'), fulldataframe = TRUE)
}
head(implist[[i]])
# analysis and pooling
fit <- with(implist, lm(read9 ~ read1.cgm + lrnprob1.cgm + read1.cgm:lrnprob1.cgm + atrisk.cgm))
# MCMC Three-Level Moderated Regression With Random Slopes
library(fdir)
library(rblimp)
library(rockchalk)
library(lme4)
library(mitml)
##################################################
# read data
##################################################
# set working directory
set()
# read imputed data from working directory
problemsolving3level <- read.table('problemsolving3level.dat', na.strings = '999')
names(problemsolving3level) <- c('school','student','wave','condition','teachexp','eslpct','hispanic',
'male','frlunch','lowach','stanmath','month', 'month7', 'probsolve', 'efficacy')
mymodel <- rblimp(
data = problemsolving3level,
clusterid = 'school student',
ordinal = 'male frlunch condition',
fixed = 'month7 male condition',
center = 'grandmean = male frlunch teachexp',
model = '
probsolve ~ month7 male frlunch teachexp condition month7*condition | month7',
simple = 'month7 | condition',
seed = 90291,
burn = 20000,
iter = 50000,
nimps = 20,
chains = 20)
# MCMC Linear Regression
library(fdir)
library(rblimp)
library(mitml)
##################################################
# read data
##################################################
# set working directory
set()
# read imputed data from working directory
behaviorachievement <- read.table('behaviorachievement.dat', na.strings = 999)
names(behaviorachievement) <- c('id','male','hispanic','riskgrp','atrisk','behsymp1','lrnprob1',
'read1','read2','read3','read9','read9grp','stanread7',
'math1','math2','math3','math9','math9grp','stanmath7')
##################################################
# fit model with mcmc (ex19.imp)
##################################################
mymodel <- rblimp(
data = behaviorachievement,
transform = 'm = ismissing(read9)',
ordinal = 'm',
model = '
focal.model:
read9 ~ read1 lrnprob1 behsymp1;
missingness.model:
m ~ read9 lrnprob1;
auxiliary.model:
stanread7 read2  ~ read9 read1 lrnprob1 behsymp1',
seed = 90291,
burn = 1000,
iter = 10000)
mymodel@output
